package rabbitmq;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

public class Producer {

	private final static String QUEUE_NAME="hello2";
	
	public static void main(String[] args) {
		try {
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("localhost");
			Connection connect = factory.newConnection();
			Channel channel = connect.createChannel();
			channel.queueDeclare(QUEUE_NAME,true,false,false,null);
			
//			String message = "hello world";
			//routingKey：路由键，#匹配0个或多个单词，*匹配一个单词，在topic exchange做消息转发用
//			channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
//			System.out.println("P [x] Sent '" + message + "'");
			
			for(int i = 0 ; i < 50000; i++){
			    String message = "Hello World! " + i;
			    channel.basicPublish("", QUEUE_NAME,MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
			    System.out.println(" [x] Sent '" + message + "'");
			}
			channel.close();
			connect.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}
	}
}
